# class base for controller
from google.appengine.ext import webapp

import random

# needed for display
from tools import collection_manager

from model import Collection

class Index(webapp.RequestHandler):
    '''
    '''
    def get(self):
        '''
        '''
        # get the language id
        locale_id = self.request.get('language')
        # get the size name
        size = self.request.get('size')

        #
        au_manager = collection_manager.CollectionManager(
                locale_id=locale_id, size=size)

        #
        query = Collection().all()
        query.filter('language =', au_manager.language)
        #
        number_of_collections = query.count()

        #
        collection_key = None

        if number_of_collections > 0:
            # random offset to get the collection
            offset = random.randint(0, number_of_collections - 1)
            # get a random collection
            collection = query.fetch(1, offset=offset)[0]
            # get the collection key
            collection_key = collection.key()

        # display the collection to show
        template = au_manager.show(collection_key=collection_key)
        return self.response.out.write(template)

    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size name
        size_key = self.request.get('size_key')

        #
        au_manager = collection_manager.CollectionManager(
                language_key=language_key, size_key=size_key)

        # retrieve the collection title of the category
        collection_key = self.request.get('collection_key')

        # retrieve the title from posting
        title = self.request.get('collection_title')

        if title and not title == '':
            # find the corresponding key
            collection_key = au_manager.get_collection_key(title)

        # display the collection to show
        template = au_manager.show(collection_key)
        return self.response.out.write(template)

class Edit(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size name
        size_key = self.request.get('size_key')

        #
        au_manager = collection_manager.CollectionManager(
                language_key=language_key, size_key=size_key)

        collection_key = None

        if not self.request.get('new') == '':
            #
            collection_key = au_manager.new()

        elif not self.request.get('edit') == '':
            # get title
            collection_key = self.request.get('collection_key')

        # retrieve the title from posting
        title = self.request.get('collection_title')
        if title and not title == '':
            # find the corresponding key
            collection_key = au_manager.get_collection_key(title)

        # display the collection to edit
        template = au_manager.edit(collection_key)
        return self.response.out.write(template)

class Save(webapp.RequestHandler):
    '''
    '''
    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size name
        size_key = self.request.get('size_key')

        #
        au_manager = collection_manager.CollectionManager(
                language_key=language_key, size_key=size_key)

        # retrieve the collection title of the category
        collection_key = self.request.get('collection_key')

        # delete
        if not self.request.get('delete') == '':
            # tools delete the collection
            au_manager.delete(collection_key)

            # go to the index page
            return self.redirect('/collection/show?language=%s&size=%s' % (
                au_manager.language.locale_id,
                au_manager.size.width))

        elif not self.request.get('new') == '':
            #
            collection_key = au_manager.new()

        # cancel
        elif not self.request.get('cancel') == '':
            # display the collection to edit
            template = au_manager.show(collection_key)
            return self.response.out.write(template)

        # we save and display the collection
        else:
            # get title
            title = self.request.get('title')

            # get info
            info = self.request.get('info')

            # tools save the collection
            au_manager.save(
                    collection_key, title, info)

        # display the collection to edit
        template = au_manager.edit(collection_key)
        return self.response.out.write(template)
